from __future__ import unicode_literals
import sys, re
from BeautifulSoup import BeautifulStoneSoup
from xml.etree.ElementTree import ElementTree

import logging

from cultureday.models import SiteSummary, SiteDig
from cultureday.management.base import MultiItemJob

class Command(MultiItemJob):
    filename=None
    f=None
    help = u'takes a list of bus stops from ... and adds them to the database'
    enc = "iso-8859-1"
    def pre_process(self, *args, **options):
        if args:
            f = open(args[0])
        else:
            f = sys.stdin
            
#        lines = f.readlines()
#        print lines;
        tree = ElementTree()
        tree.parse(f)
        tree.write("/tmp/kml.xml", self.enc)
        
#        bytes_in = self.f.read()
#        self.f.close()
        f8 = open("/tmp/kml.xml")
        xml = f8.read()
        xml = xml.decode(self.enc)
        self.soup = BeautifulStoneSoup(xml)
        self.name = self.soup.find('name')
        
        self.count=0
        super(MultiItemJob, self).pre_process()
        
    def items(self):
        digs = self.soup.findAll('placemark')
#        print digs[0].decode('utf-8')
        return digs
    
    def process(self, item):
        placemark = item
        placemark_string = u''
        name = u''
        molo_re = None
        if placemark:
            placemark_string = placemark['id']
            name = placemark.find('name').string
            description = placemark.contents
            molo_re = re.search('\((\w*)\)$', placemark_string)
            description = placemark.find(u'description').contents[0]
            coordinates = placemark.find(u'coordinates').contents
            longitude, latitude, radius = coordinates[0].split(',')
        molo_id = None
        if molo_re:
            molo_id = molo_re.group(1)
        
        if molo_id:
            site, created = SiteSummary.objects.get_or_create(museum_code=molo_id, longitude=longitude, latitude=latitude)
            site.name = name
            site.placemark = placemark_string
#            site.description = description
            site.save()
            dig, created = SiteDig.objects.get_or_create(description=description, summary=site)
        
       
x = """
<kml xmlns="http://earth.google.com/kml/2.2">
	<Document xmlns="">
		<name>Fieldwork summaries for 2008</name>
		<Folder>
			<name>Brent</name>
			<Placemark id="Cricklewood Bus Garage, 329 Edgware Road, London NW2, (CBJ07)">
				<description><![CDATA[
	<p><strong>Cricklewood Bus Garage, 329 Edgware Road, London NW2,</strong> TQ 23310 86292 MOLA (Aleksandra Cetera) evaluation September 2008 Metroline Ltd CBJ07</p><p>Following work in 2007, a single trench was excavated in the south-central area of the site.  Clean natural clay was overlain by a layer of brownish-yellow clay contaminated by diesel seepage from large metal tanks located nearby.  This was truncated by modern concrete and rubble, which included the remains of an undated clinker floor surface. No other archaeological remains were observed.</p><p><a href="http://www.molas.org.uk/pages/siteSummariesDetails.asp?year=summaries2008&amp;siteid=">See site report</a></p>]]></description>
				<name>Cricklewood Bus Garage, 329 Edgware Road, London NW2,</name>
				<Point>
					<coordinates>-0.2224669,51.5620203,0</coordinates>
				</Point>
			</Placemark>
		</Folder>
"""
